package com.skytech.tax.customsdatawarehouse.singlewindow.revokelist.domain.dao;

import com.skytech.tax.customsdatawarehouse.singlewindow.revokelist.domain.entity.CusAutoRevokeListFile;
import org.joda.time.DateTime;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author Lianhong
 * @since 2021/11/30
 */
@Repository
public class CusAutoRevokeListFileDao {

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private String selectApplicationFileSql = "SELECT file FROM sw_cus_auto_revoke_list_file WHERE decModSeqNo = ?";

    public String findByDecModSeqNo(String decModSeqNo) {
        return namedParameterJdbcTemplate.getJdbcOperations().queryForObject(selectApplicationFileSql, new Object[]{decModSeqNo}, String.class);
    }

    private String insertApplicationFileSql = "INSERT INTO sw_cus_auto_revoke_list_file (decModSeqNo, file, storageTime) VALUES (:decModSeqNo, :file, :storageTime)";

    public void save(CusAutoRevokeListFile file) {
        file.setStorageTime(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
        this.namedParameterJdbcTemplate.update(this.insertApplicationFileSql, new BeanPropertySqlParameterSource(file));
    }

    private String existsApplicationFileSql = "SELECT count(1) FROM sw_cus_auto_revoke_list_file WHERE decModSeqNo = ?";

    public boolean exists(String decModSeqNo) {
        Integer count = namedParameterJdbcTemplate.getJdbcOperations()
                .queryForObject(this.existsApplicationFileSql, new Object[]{decModSeqNo}, Integer.class);
        return count != null && count > 0;
    }
}
